package com.artPreter.controller.pc;

import com.artPreter.pojo.*;
import com.artPreter.service.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import static com.artPreter.utils.PageUtils.getPageList;

@Controller
public class IndexContorller {

    @Autowired
    private IArticleService articleService;
    @Autowired
    private IArtistService artistService;
    @Autowired
    private IArtworkService artWorkService;
    @Autowired
    private IDepartService departService;
    @Autowired
    private IArtTypeService artTypeService;
    @Autowired
    private IBannerService bannerService;
    @Autowired
    private IDepartContentSectionService departContentSectionService;
    @Autowired
    private IIndexItemRelativeService indexItemRelativeService;
    @Autowired
    private ISystemConfigService systemConfigService;
    @Autowired
    private IEventExhibitionService eventExhibitionService;
    @Autowired
    private IEventExhibitionBannerService eventExhibitionBannerService;

    @RequestMapping("/")
    public String index(Model model) {
        // 系统设置
        HashMap systemConfig = systemConfigService.getSystemConfig();
        model.addAttribute("systemConfig",systemConfig);
        // 头部菜单
        List<DepartContentSection> menuList = departContentSectionService.getMenuList();
        menuList.get(0).setIsActice(1);
        model.addAttribute("menus",menuList);

        // 活动展览
        List<EventExhibition> eventExhibitions = indexItemRelativeService.getIndexContentEventExhibitions("1b3a29a9-1c40-4dd5-9905-ba78fa6bfb24",1);
        model.addAttribute("eventExhibitions",eventExhibitions);

        // 推荐新闻 2条
        List<Article> recommendArticles = articleService.getRecommendArticles(2);
        model.addAttribute("recommendArticles",recommendArticles);
        // 首页新闻 7 条
        List<Article> indexArticles = articleService.getIndexArticles(7);
        model.addAttribute("indexArticles",indexArticles);
        // 推荐艺术家 1 条
        List<Artist> recommendArtists = artistService.getRecommendArtists(1);
        model.addAttribute("recommendArtists",recommendArtists);
        // 名家简历 7条
        List<Artist> famousArtists = artistService.getFamousArtists(7);
        model.addAttribute("famousArtists",famousArtists);
        // 首页显示艺术家
        List<Artist> indexArtists = artistService.getIndexArtists(32);
        model.addAttribute("indexArtists",indexArtists);
        // 推荐艺术品 12条
        List<Artwork> recommendArtworks = artWorkService.getRecommendArtworks(12);
        model.addAttribute("recommendArtworks",recommendArtworks);

        // 首页显示艺术品 9条
        List<Artwork> indexArtworks = artWorkService.getIndexArtworks(8);
        model.addAttribute("indexArtworks",indexArtworks);

        // 首页显示机构 4条
        List<Depart> indexDeparts = departService.getIndexDeparts(4);
        model.addAttribute("indexDeparts",indexDeparts);

        // 首页显示艺术类型
        List<ArtType> artTypes = artTypeService.getList();
        model.addAttribute("artTypes",artTypes);

        // 首页轮播
        List<Banner> banners = bannerService.getList(99);
        model.addAttribute("banners",banners);

        // 首页作品在线
        List<Artwork> onLineArtworks = indexItemRelativeService.getIndexContentArtworks("830388e7-538a-45f5-9201-cf2c67117674",8);
        model.addAttribute("onLineArtworks",onLineArtworks);

        return "pc/index";
    }

    @RequestMapping("/artist/list")
    public String index(String type, String name,String fy,String atype,Integer currentPage, Model model) throws UnsupportedEncodingException {
        if(name == null||name.equals("")||name.equals("null")){
            name = "";
        }
        if(fy == null||fy.equals("")||fy.equals("null")){
            fy = "";
        }
        if(atype == null||atype.equals("")||atype.equals("null")){
            atype = "";
        }

        if(currentPage == null){
            currentPage = 1;
        }

        // 系统设置
        HashMap systemConfig = systemConfigService.getSystemConfig();
        model.addAttribute("systemConfig",systemConfig);


        // 头部菜单
        List<DepartContentSection> menuList = departContentSectionService.getMenuList();
        model.addAttribute("menus",menuList);

        List<ArtType> artTypes = new ArrayList<>();

        menuList.get(2).setIsActice(1);
        model.addAttribute("menus",menuList);
        Artist artist = new Artist();
        artist.setName(name);
        artist.setFirstPy(fy);
        artist.setArtistType(atype);
        Integer size = 36;

        IPage<Artist> artistIPage = artistService.getPage(currentPage,size,artist);
        model.addAttribute("currentPage",artistIPage.getCurrent());
        artistIPage.getPages();
        // 分页页码
        model.addAttribute("pages",getPageList((int) artistIPage.getPages(),currentPage,5));
        // 总页数
        model.addAttribute("totalPages",artistIPage.getPages());
        // 分页数据
        model.addAttribute("artists",artistIPage.getRecords());
        model.addAttribute("keyword",name);
        System.out.println("atype"+atype);
        model.addAttribute("atype",atype);
        model.addAttribute("fy",fy);
        // 首页显示艺术类型
        artTypes = artTypeService.getList();
        model.addAttribute("artTypes",artTypes);
        return "pc/index/artist_list";

    }






    @RequestMapping("/event/list")
    public String indexEvent(String name,Integer type,Integer currentPage, Model model) {

        // 系统设置
        HashMap systemConfig = systemConfigService.getSystemConfig();
        model.addAttribute("systemConfig",systemConfig);

        if(currentPage == null){
            currentPage = 1;
        }

        // 首页轮播
        List<EventExhibitionBanner> banners = eventExhibitionBannerService.getList(99);
        model.addAttribute("banners",banners);

        // 头部菜单
        List<DepartContentSection> menuList = departContentSectionService.getMenuList();
        for (int i = 0; i < menuList.size(); i++) {
            if(menuList.get(i).getContent().equals("/event/list")){
                menuList.get(i).setIsActice(1);
            }
        }
        model.addAttribute("menus",menuList);
        // 活动展览
        EventExhibition eventExhibition = new EventExhibition();


        IPage<EventExhibition> eventExhibitionIPage = eventExhibitionService.getPage(eventExhibition,currentPage);

        // 数据
        List<EventExhibition> records = eventExhibitionIPage.getRecords();
        for (int l = 0; l < records.size(); l++) {
            if(records.get(l).getEndTime().isBefore(LocalDateTime.now())){
                records.get(l).setIsOver(1);
            }
            List<Artist> list = new ArrayList<>();
            if(records.get(l).getArtistIds()!=null){
                String[] strings1 = records.get(l).getArtistIds().split("，");
                String[] strings2 = records.get(l).getArtistNames().split("，");
                for (int z = 0; z < strings1.length; z++) {
                    if(!strings1[z].equals("")){
                        Artist artist = new Artist();
                        artist.setId(Integer.valueOf(strings1[z]));
                        artist.setName(strings2[z]);
                        list.add(artist);
                    }

                }
            }
            records.get(l).setList(list);
        }


        model.addAttribute("eventExhibitions",eventExhibitionIPage.getRecords());
        // 当前页
        model.addAttribute("currentPage",eventExhibitionIPage.getCurrent());
        // 总页数
        model.addAttribute("totalPages",eventExhibitionIPage.getPages());
        // 分页页码
        model.addAttribute("pages",getPageList((int) eventExhibitionIPage.getPages(),currentPage,5));


        return "pc/index/event_list";
    }

    @RequestMapping("/artwork/online")
    public String index(String name,Integer type,Integer currentPage, Model model) {
        Artwork artwork = new Artwork();


        if(currentPage == null){
            currentPage = 1;
        }

        // 系统设置
        HashMap systemConfig = systemConfigService.getSystemConfig();
        model.addAttribute("systemConfig",systemConfig);

        // 头部菜单
        List<DepartContentSection> menuList = departContentSectionService.getMenuList();
        model.addAttribute("menus",menuList);

        List<ArtType> artTypes = new ArrayList<>();
        menuList.get(4).setIsActice(1);
        // 首页显示艺术类型
        artTypes = artTypeService.getList();


        if(type!=null){
            for (int i = 0; i < artTypes.size(); i++) {
                if(artTypes.get(i).getId().equals(type)){
                    artwork.setArtwoktType(artTypes.get(i).getTypeId());
                }
            }
        }
        if(name == null||name.equals("")||name.equals("null")){
            name = "";
        }else{
            artwork.setName(name);
        }
        artwork.setType(-1);
        Integer artWorkSize = 16;
        IPage<Artwork> artworkIPage =  artWorkService.getPage(currentPage,artWorkSize,artwork);
        model.addAttribute("artTypes",artTypes);
        // 当前页
        model.addAttribute("currentPage",artworkIPage.getCurrent());
        // 总页数
        model.addAttribute("totalPages",artworkIPage.getPages());
        // 分页页码
        model.addAttribute("pages",getPageList((int) artworkIPage.getPages(),currentPage,5));
        // 分页数据
        model.addAttribute("artworks",artworkIPage.getRecords());
        model.addAttribute("name",name);
        model.addAttribute("type",type);
        return "pc/index/online_list";

    }
}
